package com.study.leetcode;

public class Solution_24 {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        //交换两个节点
        ListNode next = head.next;
        head.next = swapPairs(next.next);
        next.next = head;
        return next;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1,
                new ListNode(2,
                        new ListNode(3,
                                new ListNode(4,
                                        new ListNode(5,
                                                new ListNode(6,
                                                        new ListNode(7,
                                                                new ListNode(8, null)
                                                        )
                                                )
                                        )
                                )

                        )
                )
        );
        Solution_24 solution24 = new Solution_24();
        ListNode listNode1 = solution24.swapPairs(listNode);
        System.out.println("listNode1 = " + listNode1);
    }
}
